package com.icss.back.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.icss.common.servlet.BaseServlet;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.service.IProductDbService;
import com.icss.service.impl.ProductDbServiceImpl;
import com.icss.vo.MakeupCategory;
import com.icss.vo.MakeupCategoryEnum;
import com.icss.vo.MakeupProduct;

/**
 * 商品操作
 * 
 * @author 陈岩
 */
@WebServlet(urlPatterns = "/back/ProductBackServlet")
public class ProductBackServlet extends BaseServlet
{
	private IProductDbService productDbService;

	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.productDbService = new ProductDbServiceImpl();
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("cateList".equalsIgnoreCase(method))
		{
			/* cateList */
			cateList(request, response);
		} else if ("cateInsert".equalsIgnoreCase(method))
		{
			/* cateInsert */
			cateInsert(request, response);
		} else if ("cateInsertSubmit".equalsIgnoreCase(method))
		{
			/* cateInsertSubmit */
			cateInsertSubmit(request, response);
		} else if ("productList".equalsIgnoreCase(method))
		{
			/* productList */
			productList(request, response);
		} else if ("productInsert".equalsIgnoreCase(method))
		{
			/* productInsert */
			productInsert(request, response);
		} else if ("productInsertSubmit".equalsIgnoreCase(method))
		{
			/* productInsertSubmit */
			productInsertSubmit(request, response);
		}else if ("productUpdate".equalsIgnoreCase(method))
		{
			/* productUpdate */
			productUpdate(request, response);
		} else if ("productUpdateSubmit".equalsIgnoreCase(method))
		{
			/* productUpdateSubmit */
			productUpdateSubmit(request, response);
		}
	}

	/**
	 * 分类列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String cateList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<MakeupCategory> apiResponse = this.productDbService.findCondListCateService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		return "/back/product/cateList";
	}

	/**
	 * 分类添加
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String cateInsert(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 页面跳转 */
		return "/back/product/cateInsert";
	}

	/**
	 * 分类添加提交
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String cateInsertSubmit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		MakeupCategory cate = new MakeupCategory();

		/* 保存数据库 */
		ApiResponse<Object> apiResponse = this.productDbService.saveOneCateService(cate);
		request.setAttribute("response", apiResponse.toJSON());
		/* 成功,提示 */
		return apiResponse.toJSON().toJSONString();
	}

	/**
	 * 分类更新
	 * 
	 * @param request
	 * @param response
	 */
	public String cateUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<MakeupCategory> apiResponse = this.productDbService.findOneCateService(condMap);
		request.setAttribute("response", apiResponse.toJSON());

		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			return "/back/product/cateUpdate";
		}
		return "/back/product/cateInfo";
	}

	/**
	 * 分类更新提交
	 * 
	 * @param request
	 * @param response
	 */
	public String cateUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<MakeupCategory> apiResponse = this.productDbService.findOneCateService(condMap);
		request.setAttribute("response", apiResponse.toJSON());

		MakeupCategory cate = apiResponse.getDataOneJava();

		boolean flag = false;
		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			/* 需要返回json */
			String name = request.getParameter("name");
			String content = request.getParameter("content");
			String status = request.getParameter("status");

			/* 赋值 */
			cate.setName(name);
			cate.setContent(content);
			cate.setStatus(Byte.valueOf(status));

			flag = true;
		}

		ApiResponse<Object> apiResponseDb = new ApiResponse<Object>();
		if (flag)
		{
			apiResponseDb = this.productDbService.updateOneCateService(cate);
		}

		/* 成功,提示 */
		JSONObject resultJSON = apiResponseDb.toJSON();
		return resultJSON.toJSONString();
	}

	/**
	 * 商品列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String productList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);

		String cateId = request.getParameter("cateId");
		if (cateId == null)
		{
			cateId = "";
		}
		request.setAttribute("cateId", cateId);
		condMap.put("cateId", cateId);

		String dataType = request.getParameter("dataType");
		if (dataType == null)
		{
			dataType = "";
		}
		request.setAttribute("dataType", dataType);
		condMap.put("dataType", dataType);

		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<MakeupProduct> apiResponse = this.productDbService.findCondListProductService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());

		/* 查询所有的分类;只查询商品的分类 */
		condMap.clear();
		condMap.put("status", MakeupCategoryEnum.STATUS_ENABLE.getStatus());
		ApiResponse<MakeupCategory> cateApiResponse = this.productDbService.findCondListCateService(null, condMap);
		request.setAttribute("cateResponse", cateApiResponse.toJSON());
		return "/back/product/productList";
	}

	/**
	 * 商品添加
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String productInsert(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 准备路径 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询所有的分类;只查询商品的分类 */
		condMap.clear();
		condMap.put("status", MakeupCategoryEnum.STATUS_ENABLE.getStatus());
		ApiResponse<MakeupCategory> cateApiResponse = this.productDbService.findCondListCateService(null, condMap);
		request.setAttribute("cateResponse", cateApiResponse.toJSON());
		return "/back/product/productInsert";
	}

	/**
	 * 商品添加提交
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String productInsertSubmit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		MakeupProduct product = new MakeupProduct();
		
		/* 保存数据库 */
		ApiResponse<Object> apiResponse = this.productDbService.saveOneProductService(product);
		request.setAttribute("response", apiResponse.toJSON());
		/* 成功,提示 */
		JSONObject resultJSON = apiResponse.toJSON();
		return resultJSON.toJSONString();
	}

	/**
	 * 商品更新
	 * 
	 * @param request
	 * @param response
	 */
	public String productUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<MakeupProduct> apiResponse = this.productDbService.findOneProductService(condMap);
		request.setAttribute("response", apiResponse.toJSON());

		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			/* 查询所有的分类 */
			/* 查询所有的分类;只查询商品的分类 */
			condMap.clear();
			condMap.put("status", MakeupCategoryEnum.STATUS_ENABLE.getStatus());
			ApiResponse<MakeupCategory> cateApiResponse = this.productDbService.findCondListCateService(null, condMap);
			request.setAttribute("cateResponse", cateApiResponse.toJSON());
			return "/back/product/productUpdate";
		}
		return "/back/product/productInfo";
	}

	/**
	 * 商品更新提交
	 * 
	 * @param request
	 * @param response
	 */
	public String productUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<MakeupProduct> apiResponse = this.productDbService.findOneProductService(condMap);
		request.setAttribute("response", apiResponse.toJSON());

		MakeupProduct product = apiResponse.getDataOneJava();

		boolean flag = false;
		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			/* 需要返回json */
			String categoryId = request.getParameter("categoryId");
			String name = request.getParameter("name");
			String brief = request.getParameter("brief");
			String price = request.getParameter("price");
			String content = request.getParameter("content");
			String status = request.getParameter("status");

			/* 赋值 */
			product.setCategoryId(Integer.valueOf(categoryId));
			product.setName(name);
			product.setBrief(brief);
			product.setPrice(Double.valueOf(price));
			product.setContent(content);
			product.setStatus(Byte.valueOf(status));

			String enableTimeStr = request.getParameter("enableTimeStr");
			product.setEnableTime(this.dateFormatUtil.strDateTime(enableTimeStr));

			flag = true;
		}

		ApiResponse<Object> apiResponseDb = new ApiResponse<Object>();
		if (flag)
		{
			apiResponseDb = this.productDbService.updateOneProductService(product);
		}

		/* 成功,提示 */
		JSONObject resultJSON = apiResponseDb.toJSON();
		return resultJSON.toJSONString();
	}
}